﻿using aotto.cn.csharp.awiz.api.components;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using static com.aotto.cn.csharp.awiz.api.BitmapToVideoConverter;
/**
* Author: Ziyi Wang, Aotto co.,ltd
* Created at Sep. 26th, 2021
* Environment: .NET Framework 4.0 Class Library, built on Visual Studio 2019
* Main Feature: Build a video stream from <Bitmap> objects.
*/
namespace com.aotto.cn.csharp.awiz.api
{
    public partial class BitmapToVideoConverter
    {
        /// <summary>
        /// 在一个线程中将一组Bitmap对象转换成avi视频。需要在构造器中手动指定framerate。
        /// </summary>
        /// <param name="bitmaps">一组Bitmap实例</param>
        /// <param name="outputPath">Avi视频的输出路径（路径+文件名）</param>
        /// <param name="actOptions">新建或保存文件</param>
        /// <returns>状态码。成功为true，否则为false</returns>
        public void Generate(Bitmap[] bitmaps, string outputPath, ActionOptions actOptions)
        {
            //if (!outputPath.ToLower().Contains(".avi")) throw new Exception("Invalid Extension Name.");
            try
            {
                AviManager aviManager = new AviManager(outputPath, (actOptions.ToString() == "Open"));
                VideoStream aviStream = aviManager.AddVideoStream(false, this.framerate, bitmaps[0]);
                foreach (Bitmap bitmap in bitmaps)
                {
                    aviStream.AddFrame(bitmap);
                    bitmap.Dispose();
                }
                aviManager.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
